home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7400 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.3 KB  |  143 lines

  1. Newsgroups: comp.software-eng,comp.unix.solaris,comp.unix.programmer,comp.windows.x,comp.windows.x.motif,comp.lang.c,comp.lang.c++,comp.lang.tcl,comp.object,comp.lang.fortran
  2. Path: mozart.slac.stanford.edu!user
  3. From: ljm@slac.stanford.edu (Leonard J. Moss)
  4. Subject: Re: structures as formal parameters
  5. Message-ID: <ljm-2202961835270001@mozart.slac.stanford.edu>
  6. Sender: news@unixhub.SLAC.Stanford.EDU
  7. Organization: Stanford Linear Accelerator Center
  8. X-Newsreader: Yet Another NewsWatcher 2.2.0b4
  9. References: <4fut4m$h5t@Dortmund.Germany.EU.net> <31267EED.4874066A@holisticmath.com> <4g6102$lql@portal.gmu.edu>
  10. Distribution: inet
  11. Date: Fri, 23 Feb 1996 02:35:27 GMT
  12.  
  13. In article <4g6102$lql@portal.gmu.edu>, jscheibl@osf1.gmu.edu (Jack W
  14. Scheible) wrote:
  15.  
  16. >I am trying to pass structures to subroutines.  My problem is that,
  17. >when I try to write the interface, it will not recognize previously
  18. >declared types.  I can put the type definition into the interface, but
  19. >then it is not recognized outside the interface.
  20. >
  21. >      PROGRAM JUNK
  22. >
  23. >      TYPE CARTESIAN
  24. >         REAL*8 X
  25. >         REAL*8 Y
  26. >      END TYPE CARTESIAN
  27. >
  28. >      INTERFACE
  29. >         SUBROUTINE CART_TO_POLAR ( XY, RTHETA )
  30. >            TYPE POLAR
  31. >               REAL*8 R
  32. >               REAL*8 THETA
  33. >            END TYPE POLAR
  34. >            TYPE (CARTESIAN), INTENT(IN) :: XY  ! CARTESIAN Not Declared
  35. >            TYPE (POLAR), INTENT(OUT):: RTHETA  ! Just fine
  36. >          END SUBROUTINE CART_TO_POLAR
  37. >      END INTERFACE
  38. >
  39. >      TYPE (CARTESIAN) XY   ! Just fine
  40. >      TYPE (POLAR) RTHETA   ! POLAR Not Declared
  41. >
  42. >      END
  43. >
  44. >Is there any way to use structures in an interface without declaring
  45. >the structures twice?
  46.  
  47. Yes -- put the type definitions in a module; for example:
  48.  
  49.       MODULE COORD_TYPES
  50.       TYPE CARTESIAN
  51.          REAL*8 X
  52.          REAL*8 Y
  53.       END TYPE CARTESIAN
  54.       TYPE POLAR
  55.          REAL*8 R
  56.          REAL*8 THETA
  57.       END TYPE POLAR
  58.       END MODULE COORD_TYPES
  59.  
  60.       PROGRAM JUNK
  61.       USE COORD_TYPES
  62.       INTERFACE
  63.          SUBROUTINE CART_TO_POLAR ( XY, RTHETA )
  64.             USE COORD_TYPES
  65.             TYPE (CARTESIAN), INTENT(IN) :: XY  ! CARTESIAN Not Declared
  66.             TYPE (POLAR), INTENT(OUT):: RTHETA  ! Just fine
  67.           END SUBROUTINE CART_TO_POLAR
  68.       END INTERFACE
  69.       TYPE (CARTESIAN) XY   ! Just fine
  70.       TYPE (POLAR) RTHETA   ! POLAR Not Declared
  71. !     Program code...
  72.       END
  73.  
  74. Note that you must also USE the module in the INTERFACE block (even though
  75. there's already a USE in your main program).  Remember that the INTERFACE
  76. block is intended to describe to the caller all the characteristics of an
  77. independently compiled procedure.  Since the latter doesn't have a host
  78. from which to inherit things like type definitions or IMPLICIT mappings,
  79. the INTERFACE block can't inherit from it's host either.
  80.  
  81. Lest Loren take me to task, let me point out that a much better solution
  82. is to put _all_ your procedures and type definitions in modules and
  83. dispense with INTERFACE blocks altogether (except for overloading).  For
  84. example,
  85.  
  86.       MODULE COORD_SYSTEMS
  87.       TYPE CARTESIAN
  88.          REAL*8 X
  89.          REAL*8 Y
  90.       END TYPE CARTESIAN
  91.       TYPE POLAR
  92.          REAL*8 R
  93.          REAL*8 THETA
  94.       END TYPE POLAR
  95.       CONTAINS
  96.          SUBROUTINE CART_TO_POLAR( XY, RTHETA )
  97.             TYPE (CARTESIAN), INTENT(IN) :: XY
  98.             TYPE (POLAR), INTENT(OUT):: RTHETA
  99. !           Code to do conversion...
  100.          END SUBROUTINE CART_TO_POLAR
  101.          SUBROUTINE POLAR_TO_CART( RTHETA, XY )
  102.             TYPE (POLAR), INTENT(OUT):: RTHETA
  103.             TYPE (CARTESIAN), INTENT(IN) :: XY
  104. !            Code to do conversion...
  105.          END SUBROUTINE POLAR_TO_CART
  106.       END MODULE COORD_SYSTEMS
  107.  
  108.       PROGRAM JUNK
  109.       USE COORD_SYSTEMS
  110.       TYPE (CARTESIAN) XY
  111.       TYPE (POLAR) RTHETA
  112. !     Program code...
  113.       END
  114.  
  115. Notice that the subroutine definitions now _can_ inherit the type
  116. definitions from their host, and the caller can bring in both type
  117. definitions and procedure interfaces with a simple USE statement.
  118.  
  119. A few other points:
  120.  
  121.    o REAL*8 is non-standard (I assume it's an accepted extension on your
  122.      compiler)
  123.    o If for some reason you don't choose to put TYPE definitions in a module,
  124.      but instead duplicate the type definitions where needed, the standard
  125.      requires that you put the SEQUENCE keyword in each type definition.
  126.    o If for some reason you don't choose to put the actual code for your
  127.      procedures in the module (e.g., it's proprietary and you don't want to
  128.      distrubute the source), you can still put the interface blocks in a 
  129.      module so the user doesn't need to embed difficult to maintain INTERFACE 
  130.      blocks in his/her code.  However, in this case you must put the type 
  131.      definitions and interface blocks in separate modules, since INTERFACE 
  132.      blocks cannot (as we've seen above) inherit the type definitions from
  133.      their host, and also cannot contain a USE for the MODULE in which they
  134.      occur.
  135.    o If you eventually want to overload assignment with conversion 
  136.      subroutines like those above, the order of the dummy arguments must be
  137.      reversed.
  138.  
  139. -- 
  140. Leonard J. Moss <ljm@slac.stanford.edu>  | My views don't necessarily
  141. Stanford Linear Accelerator Center       | reflect those of SLAC,
  142. MS 97; P.O. Box 4349; Stanford, CA 94309 | Stanford or the DOE
  143.